using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Runtime.Serialization;
using System.Security.Cryptography;
using System.Text;
using Buddy.Auth;
using log4net;
using Newtonsoft.Json;
using Triton.Common;
using Triton.Common.LogUtilities;

namespace Triton.Bot.Settings
{
	public class GlobalSettings : JsonSettings
	{
		private static class Class214
		{
			private static readonly byte[] byte_0 = Encoding.ASCII.GetBytes("o6806642kbM7c5");

			public static string smethod_0(string string_0, string string_1)
			{
				if (string.IsNullOrEmpty(string_0))
				{
					throw new ArgumentNullException("plainText");
				}
				if (string.IsNullOrEmpty(string_1))
				{
					throw new ArgumentNullException("sharedSecret");
				}
				RijndaelManaged rijndaelManaged = null;
				try
				{
					Rfc2898DeriveBytes rfc2898DeriveBytes = new Rfc2898DeriveBytes(string_1, byte_0);
					rijndaelManaged = new RijndaelManaged();
					rijndaelManaged.Key = rfc2898DeriveBytes.GetBytes(rijndaelManaged.KeySize / 8);
					ICryptoTransform transform = rijndaelManaged.CreateEncryptor(rijndaelManaged.Key, rijndaelManaged.IV);
					using MemoryStream memoryStream = new MemoryStream();
					memoryStream.Write(BitConverter.GetBytes(rijndaelManaged.IV.Length), 0, 4);
					memoryStream.Write(rijndaelManaged.IV, 0, rijndaelManaged.IV.Length);
					using (CryptoStream stream = new CryptoStream(memoryStream, transform, CryptoStreamMode.Write))
					{
						using StreamWriter streamWriter = new StreamWriter(stream);
						streamWriter.Write(string_0);
					}
					return Convert.ToBase64String(memoryStream.ToArray());
				}
				finally
				{
					rijndaelManaged?.Clear();
				}
			}

			public static string smethod_1(string string_0, string string_1)
			{
				if (string.IsNullOrEmpty(string_0))
				{
					throw new ArgumentNullException("cipherText");
				}
				if (string.IsNullOrEmpty(string_1))
				{
					throw new ArgumentNullException("sharedSecret");
				}
				RijndaelManaged rijndaelManaged = null;
				try
				{
					Rfc2898DeriveBytes rfc2898DeriveBytes = new Rfc2898DeriveBytes(string_1, byte_0);
					using MemoryStream memoryStream = new MemoryStream(Convert.FromBase64String(string_0));
					rijndaelManaged = new RijndaelManaged();
					rijndaelManaged.Key = rfc2898DeriveBytes.GetBytes(rijndaelManaged.KeySize / 8);
					rijndaelManaged.IV = smethod_2(memoryStream);
					ICryptoTransform transform = rijndaelManaged.CreateDecryptor(rijndaelManaged.Key, rijndaelManaged.IV);
					using CryptoStream stream = new CryptoStream(memoryStream, transform, CryptoStreamMode.Read);
					using StreamReader streamReader = new StreamReader(stream);
					return streamReader.ReadToEnd();
				}
				finally
				{
					rijndaelManaged?.Clear();
				}
			}

			private static byte[] smethod_2(Stream stream_0)
			{
				byte[] array = new byte[4];
				if (stream_0.Read(array, 0, array.Length) != array.Length)
				{
					throw new SystemException("Stream did not contain properly formatted byte array");
				}
				byte[] array2 = new byte[BitConverter.ToInt32(array, 0)];
				if (stream_0.Read(array2, 0, array2.Length) != array2.Length)
				{
					throw new SystemException("Did not read byte array properly");
				}
				return array2;
			}
		}

		private static GlobalSettings globalSettings_0;

		private static readonly ILog ilog_0 = Logger.GetLoggerInstanceForType();

		private List<Region> list_0;

		private Region region_0;

		private bool bool_0;

		private string string_1;

		private bool bool_1;

		private List<string> configList;

		private string string_2;

		private bool bool_2;

		public static GlobalSettings Instance => globalSettings_0 ?? (globalSettings_0 = new GlobalSettings());

		[DefaultValue(false)]
		public bool DebugLastTask
		{
			get
			{
				return bool_2;
			}
			set
			{
				if (!value.Equals(bool_2))
				{
					bool_2 = value;
					NotifyPropertyChanged(() => DebugLastTask);
				}
			}
		}

		public string LastUsedConfiguration
		{
			get
			{
				return string_2;
			}
			set
			{
				if (!value.Equals(string_2))
				{
					string_2 = value;
					NotifyPropertyChanged(() => LastUsedConfiguration);
				}
			}
		}

		public List<string> Configurations
		{
			get
			{
				return configList;
			}
			set
			{
				if (!value.Equals(configList))
				{
					configList = value;
					NotifyPropertyChanged(() => Configurations);
				}
			}
		}

		[DefaultValue(true)]
		public bool AuthLogin
		{
			get
			{
				return bool_0;
			}
			set
			{
				if (!value.Equals(bool_0))
				{
					bool_0 = value;
					NotifyPropertyChanged(() => AuthLogin);
				}
			}
		}

		[DefaultValue(false)]
		public bool DisableUpdateCheck
		{
			get
			{
				return bool_1;
			}
			set
			{
				if (!value.Equals(bool_1))
				{
					bool_1 = value;
					NotifyPropertyChanged(() => DisableUpdateCheck);
				}
			}
		}

		[DefaultValue(Region.China)]
		public Region AuthRegion
		{
			get
			{
				return region_0;
			}
			set
			{
				if (!value.Equals(region_0))
				{
					region_0 = value;
					NotifyPropertyChanged(() => AuthRegion);
					ilog_0.InfoFormat("[AuthRegion] {0}.", region_0);
				}
			}
		}

		public string LoginKey
		{
			get
			{
				return string_1;
			}
			set
			{
				if (!value.Equals(string_1))
				{
					string_1 = value;
					NotifyPropertyChanged(() => LoginKey);
				}
			}
		}

		[JsonIgnore]
		public List<Region> AuthRegions
		{
			get
			{
				List<Region> list = list_0;
				if (list == null)
				{
					List<Region> obj = new List<Region>
					{
						Region.BestLatency,
						Region.Europe,
						Region.NorthAmerica,
						Region.China,
						Region.SoutheastAsia
					};
					List<Region> list2 = obj;
					list_0 = obj;
					list = list2;
				}
				return list;
			}
		}

		public GlobalSettings()
			: base(JsonSettings.GetSettingsFilePath("Global", "GlobalSettings.json"))
		{
			if (Configurations == null)
			{
				Configurations = new List<string> { "Default" };
				string_2 = "Default";
			}
			if (Configurations.Count == 0)
			{
				Configurations.Add("Default");
				string_2 = "Default";
			}
		}

		[OnSerializing]
		internal void method_0(StreamingContext streamingContext_0)
		{
			if (!string.IsNullOrEmpty(LoginKey))
			{
				LoginKey = Class214.smethod_0(LoginKey, "hbseckey");
			}
		}

		[OnSerialized]
		internal void method_1(StreamingContext streamingContext_0)
		{
			if (!string.IsNullOrEmpty(LoginKey))
			{
				LoginKey = Class214.smethod_1(LoginKey, "hbseckey");
			}
		}

		[OnDeserialized]
		internal void method_2(StreamingContext streamingContext_0)
		{
			method_1(streamingContext_0);
		}
	}
}
